<?php

namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class PubWechatController extends AdminbaseController {
    
    public function index()
    {    	if(IS_POST){
    		$p = I("post.");
    		$where = "1";
	    	
	    	if(!empty($p['so_start'])){
	    		$so_start=strtotime($p['so_start']);
	    		$where.=" and create_time>$so_start";
	    		$this->assign('so_start',$p['so_start']);
	    	}
	    	if(!empty($p['so_end'])){
	    		$so_end=strtotime($p['so_end']);
	    		$where.=" and create_time<$so_end";
	    		$this->assign('so_end',$p['so_end']);
	    	}
	    	if(!empty($p['so_name'])){
	    		$where.=" and user_name like '%{$p['so_name']}%'";
	    		$this->assign('so_name',$p['so_name']);
	    	}
    	}else{
    		$state = I("get.state_id");
    		$uid = I("get.uid");
    		if($state !=""){
    			$where['status']=$state;
    		}
    		if($uid !=""){
    			$where['uid']=$uid;
    		}
    	}
    	$where["wechatType"]=1; //为服务号
    	$count = M("pubwechat")->alias("P")->join("__USER__ AS U on U.id=P.uid")->where($where)->count();
    	$page = $this->page($count, 20);
    	$content =  M("pubwechat")->alias("P")->field(" *, P.id as pid")->join("__USER__ AS U on U.id=P.uid")->where($where)->order("posttime desc")->limit($page->firstRow . ',' . $page->listRows)->select();
    	$this->assign('state_id',$state);//状态
    	$this->assign('content',$content);
    	$this->assign("page", $page->show('Admin'));

        $this->display();
    }
    public function shenhe()
    {
        if(IS_POST)
        {
        	$_POST["auttime"]=time();
          $result=M("pubwechat")->save($_POST);     
          if($result){ $this->success("修改成功"); }else{ $this->error("修改失败");}
        }
        $id=I("get.id",0,"intval");
        if($id==0){$this->error("参数错误");}
        $where["id"]=$id;
        $data=M("pubwechat")->where($where)->find();
       if(!$data){$this->error("不存在数据");}
       $user=M("user")->where("id=$data[uid]")->find();
       //获取类型信息
       
       $comm=M("PubAssort")->order("listorder desc")->getField("id,t_name");
       
       $this->assign("comm",$comm);
       $this->assign("data",$data);
       $this->assign("user",$user);
       $this->display();
    }
    //常见分类列表
    public function CommonClass()
    {
    	$this->lists=M("PubAssort")->order("listorder asc")->select();
    	$this->display("common");
    }
    //添加
    public function add(){
    	if(IS_POST){
    		$p=I("post.");
    		if(M('PubAssort')->add($p)){
    			$this->success("添加成功");
    		}else{
    			$this->error("添加失败");
    		}
    	}else{
    		$this->display();
    	}
    
    }
    
    //修改
    public function edit(){
    	$id=I("get.id");
    	if(IS_POST){
    
    		if(M("PubAssort")->where(array("id"=>$id))->save(I("post."))){
    			$this->success("更新成功");
    		}else{
    			$this->error("更新失败");
    		}
    	}else{
    		$this->res= M("PubAssort")->find($id);
    		$this->display();
    	}
    	 
    }
    //删除
    public function del(){
    	$id=I("get.id");
    	if(M("PubAssort")->delete($id)){
    		$this->success("删除成功");
    	}else{
    		$this->error("删除失败");
    	}
    }
    //排序
    public function sortlist(){
    	if($this->_listorders(M("PubAssort"))){
    		$this->success("更新成功");
    	}else{
    		$this->error("更新失败");
    	}
    	 
    }
    
    
}